layui.config({
    base: '../../../eui/plugins/layui-extend/' //这是你存放拓展模块的根目录
});

layui.use(['form','table'], function () {
	var table = layui.table;
    var form = layui.form;
    var id = getParam("id");
    dict2Select("yesNo","expable"); 
	io.send("/mm/datasources?pageNo=1&pageSize=-1","GET",null,function(res){
		var data=res.data.list;
		var map=new Map();
		for(var i in data){
			var d=data[i];
    		map.set(d.dsId.toString(),d.dsId);
		}
		map2Select(map,'datasourceId');
	    form.render();
	},false,false,false,false);
    if(!isNull(id)){
    	io.get('/mm/datastreams/' + id,function(res){
    		var data = res.data;
            var form = layui.form;
            //表单初始赋值
            form.val('lForm', data)
            form.render();
    	});
    }
		table.render({
	          elem: '#fieldTable'
	        , url: appendCtx('/mm/datastreams/'+ (isNull(id)?0:id) +'/fields')
	        , page: false
			, height: '700'
			, toolbar: '#tablebar'
			, limit :1000
	        , cols: [[ //表头
	            {field: 'id', title: 'ID',  align: 'center', width: 70},
	            {field: 'innerEName', title: '英文名称', align: 'center',width: 160},
	            {field: 'innerCName', title: '中文名称', align: 'center',width: 200},
	            {field: 'externalType', title: '源类型', align: 'center',width: 100,templet:
	            	function(d)
	            	{
	            		return translate("fieldType",d.externalType);
	            	}
	            },
	            {field: 'innerType', title: '执行类型', align: 'center',width: 100,templet:
	            	function(d)
	            	{
	            		return translate("fieldType",d.innerType);
	            	}
	            },
	            {field: '', title: '必填', align: 'center',width: 60,templet:
	            	function(d)
	            	{
	            		return translate("yesNo",d.notNull);
	            	}
	            },
	            {field: '', title: '校验', align: 'center',width: 60,templet:
	            	function(d)
	            	{
	            		return translate("yesNo",d.checkValue);
	            	}
	            },
	            {field: 'creatorName', title: '创建人',align: 'center',width: 100} 
				,{fixed: 'right', width:120, align:'center', toolbar: '#colbar'}
	        ]]
	        , parseData: function (res) { //res 即为原始返回的数据
	            return {
	                "code": res.code, //解析接口状态
	                "msg": res.msg, //解析提示文本
	                "data": res.data //解析数据列表
	                ,"attr":res.attr
	            };
	        }
	    });
	table.on('tool(fieldTable)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
		var layEvent = obj.event;
		  if(layEvent === 'del'){ //删除
		      obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
		  }
	});

	table.on('toolbar(fieldTable)', function(obj){
	  switch(obj.event){
	    case 'add':
			var addFieldName=$('#addFieldName').val();
			if(isNull(addFieldName)){
				layer.msg("请输入字段中文名称")
			}
			addFieldName=addFieldName.replace(/\s+/g,"");
			var allData = table.cache["fieldTable"];
			for(d in allData){
				var dd=allData[d];
				if(dd.innerCName===addFieldName&&!isNull(dd.id)){
					layer.msg("已经关联该字段")
					return;
				}
			}
			io.send("/mm/fields/cname","POST",JSON.stringify({"innerCName":addFieldName}),function(res){
				if(isNull(res.data)){
					layer.msg("不存在的字段")
				}else{
					var allData = table.cache["fieldTable"];
			        var data =res.data;
			        allData.push(data);
			       //刷新
			        table.reload('fieldTable', {
			            url: '',
			            data: allData
			        });
				}
			},false,false,false,true);
	    break;
	  };
	});
	
    //监听提交
    form.on('submit(save)', function (form) {
		var allData = table.cache["fieldTable"];
		var fieldList = [];
		for(d in allData){
			var dd=allData[d];
			if(!isNull(dd.id)){
				fieldList.push(dd.id);
			}
		}
        var data=form.field;
		data.fieldList=fieldList;
		io.post('/mm/datastreams',JSON.stringify(data),function(res){
	               setTimeout(function(){return closePage();},1000);
	               window.parent.refreshTableData();
		});
		return false;
    });
});